package com.hanter.android.radwidget.statusbar;

import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * 魅族Meizu手机Flyme系统状态栏适配
 * 参考：http://open-wiki.flyme.cn/doc-wiki/index#id?76
 */
public class FlymeAdapter implements StatusBarModeAdapter {

    private static final String TAG = "FlymeAdapter";

    @Override
    public boolean isSupportLightMode() {
        return false;
    }

    @Override
    public boolean setStatusBarMode(Window window, boolean lightMode) {
        boolean result = false;
        try {
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (lightMode) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
            result = true;
        } catch (Exception e) {
            Log.e(TAG, "setStatusBarDarkIcon: failed");
        }
        return result;
    }

}
